﻿// License: The MIT License (MIT) Copyright (c) 2011..2012 Barend Gehrels

// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Controls;
using Stormy;
using System.Windows;

namespace GrooveSharp
{

    public partial class TrackPopupInfo : UserControl
    {
        FlagManager m_flagManager = new FlagManager(0);

        private class PlayInfo
        {
            public DateTime? PlayDate { get; set; }
            public TimeSpan? Span { get; set; }
            public int? Rating { get; set; }
            public string PlayMachine { get; set; }
            public string UserName { get; set; }
        }

        private IList<PlayInfo> m_playList;

        public TrackPopupInfo()
        {
            InitializeComponent();
        }

        public void Select(int id, string filename)
        {
            this.selectedTrack.Text = filename;

            if (id > 0)
            {
                m_playList = Db.Connection().Select<PlayInfo>(String.Format(
                    @"
                        select 1 as source,datetime(play_date) as play_date,play_duration,0 as rating_value,user_name from plays t {1} where t.track_id={0}
                        union
                        select 2 as source,datetime(rating_date),0,rating_value,user_name from ratings t {1} where t.track_id={0}
                    ", id, "left join users u on t.user_id=u.user_id"),
                        (reader) => new PlayInfo()
                        {
                            PlayDate = reader.AsDate("play_date"),
                            Rating = reader.AsInteger("rating_value"),
                            Span = reader.AsTimeSpan("play_duration"),
                            UserName = reader.AsString("user_name")
                        }
                     ).ToList();

                if (filename == null)
                {
                    string path = Db.SelectFirstOrThrow<string>(String.Format(
                        @"
                            select p.path_folder || '\' || t.folder_name || '\' || t.file_name as pathname
                            from tracks t
                            join paths p on t.path_id=p.path_id
                            where track_id={0}
                        ", id));

                    // Get rid of spurios double backslashes, \\, if any
                    this.selectedTrack.Text = System.IO.Path.GetFullPath(path);
                }
            }
            else
            {
                m_playList = null;
            }

            listViewTrackInfo.ItemsSource = m_playList;

            if (id != m_flagManager.TrackId)
            {
                m_flagManager.save();
                m_flagManager = new FlagManager(id);
                checkListBox.ItemsSource = m_flagManager.flags;
            }
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            m_flagManager.flagChanged = true;
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            m_flagManager.flagChanged = true;
        }

    }
}
